package mr.ali.webvideo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mr.ali.webvideo.Exception.ServiceException;
import mr.ali.webvideo.mapper.CollectionTagMapper;
import mr.ali.webvideo.mapper.CollectionUrlMapper;
import mr.ali.webvideo.model.Collection;
import mr.ali.webvideo.mapper.CollectionMapper;
import mr.ali.webvideo.model.CollectionImg;
import mr.ali.webvideo.model.CollectionTag;
import mr.ali.webvideo.model.CollectionYurt;
import mr.ali.webvideo.service.ICollectionImgService;
import mr.ali.webvideo.service.ICollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mr.ali.webvideo.service.ICollectionYurtService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr-ali
 * @since 2025-08-16
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements ICollectionService {

    @Resource
    private CollectionMapper collectionMapper;

    @Resource
    private CollectionTagMapper collectionTagMapper;

    @Resource
    private CollectionUrlMapper collectionUrlMapper;

    @Resource
    private ICollectionImgService collectionImgService;

    @Resource
    private ICollectionYurtService yurtService;

    /**
     * 正常排序查询
     */
    @Override
    public PageInfo<Collection> findAll(Integer pageNum, Integer pageSize, Integer type, String order) {

        // 判断翻页参数是否为空
        if (pageNum == null || pageSize == null) {
            // 抛出翻页参数错误异常
            throw ServiceException.GONE("翻页参数错误");
        }
        // 创建查询条件包装器
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        // 按照创建时间降序排序
        wrapper.eq("displayed", 1);
        if (type != 0) {
            wrapper.eq("type", type);
        }
        wrapper.orderByDesc("topping");
        if (order != null) {
            wrapper.orderByDesc(order);
        }
        wrapper.orderByDesc("gmt_create");
        // 使用PageHelper进行分页
        PageHelper.startPage(pageNum, pageSize);
        // 根据查询条件获取视频列表
        List<Collection> videoList = collectionMapper.selectList(wrapper);
        for (Collection video : videoList) {
            List<CollectionTag> tag = collectionTagMapper.VideoSortName(video.getId());
            Integer episodes = collectionUrlMapper.CountByUrlVideoId(video.getId());
            video.setEpisodes(episodes);
            video.setTags(tag);
        }

        return new PageInfo<>(videoList);
    }

    /**
     * ID查询
     */
    @Override
    public List<Collection> videoTagsById(Integer id) {
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        wrapper.eq("displayed", 1);
        wrapper.orderByDesc("topping");
        // 根据查询条件获取视频列表
        List<Collection> videoList = collectionMapper.selectList(wrapper);
        for (Collection video : videoList) {
            List<CollectionTag> tag = collectionTagMapper.VideoSortName(video.getId());
            CollectionYurt yurt = yurtService.findBySortID(video.getYurt());
            Integer episodes = collectionUrlMapper.CountByUrlVideoId(video.getId());
            video.setEpisodes(episodes);
            video.setYurts(yurt.getName());
            video.setTags(tag);
        }
        return videoList;
    }

    /**
     * 随机查询
     */
    @Override
    public PageInfo<Collection> findAllByOrderLast(Integer pageNum, Integer pageSize, Integer type, String order, Boolean yesVideo) {

        // 判断翻页参数是否为空
        if (pageNum == null || pageSize == null) {
            // 抛出翻页参数错误异常
            throw ServiceException.GONE("翻页参数错误");
        }
        // 创建查询条件包装器
        LambdaQueryWrapper<Collection> wrapper = new LambdaQueryWrapper<>();
        // 按照创建时间降序排序
        wrapper.eq(Collection::getDisplayed, 1);
        if (type != 0) {
            wrapper.eq(Collection::getType, type);
        }
        if (order != null) {
            wrapper.last("ORDER BY RAND(), bifen DESC");
        } else {
            wrapper.last("ORDER BY RAND()");
        }

        // 使用PageHelper进行分页
        PageHelper.startPage(pageNum, pageSize);
        // 根据查询条件获取视频列表
        List<Collection> videoList = collectionMapper.selectList(wrapper);
        for (Collection video : videoList) {
            CollectionYurt yurt = yurtService.findBySortID(video.getYurt());
            List<CollectionTag> tag = collectionTagMapper.VideoSortName(video.getId());
            Integer episodes = collectionUrlMapper.CountByUrlVideoId(video.getId());
            video.setEpisodes(episodes);
            video.setTags(tag);
            video.setYurts(yurt.getName());
            if (yesVideo) {
                List<CollectionImg> images = collectionImgService.findByIdAll(video.getId());
                video.setImages(images);
            }
        }

        return new PageInfo<>(videoList);
    }

    /**
     * 标题来模糊查询
     */
    @Override
    public PageInfo<Collection> searchCollections(String name, int pageNum, int pageSize) {
        LambdaQueryWrapper<Collection> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(Collection::getName, name);
        }
        wrapper.eq(Collection::getDisplayed, 1);
        wrapper.orderByDesc(Collection::getTopping);
        wrapper.orderByDesc(Collection::getGmtCreate);
        PageHelper.startPage(pageNum, pageSize);
        List<Collection> list = collectionMapper.selectList(wrapper);
        for (Collection video : list) {
            List<CollectionTag> tag = collectionTagMapper.VideoSortName(video.getId());
            Integer episodes = collectionUrlMapper.CountByUrlVideoId(video.getId());
            CollectionYurt yurt = yurtService.findBySortID(video.getYurt());
            video.setEpisodes(episodes);
            video.setTags(tag);
            video.setYurts(yurt.getName());
        }

        return new PageInfo<>(list);
    }

    @Override
    public List<Collection> findAllSeriesId(Integer seriesId) {
        return collectionMapper.findAllSeriesId(seriesId);
    }

}
